iT邦幫忙

2021 iThome 鐵人賽

DAY 12
0

挑戰目標: MockNative Camp


昨天我們將SignupReq.java給建立完,現在要開始做繁雜的工作,Object間的轉換,不知道有沒有更快速的方式。
首先要先在Teachers.java中加入一個method

package com.mock.nativecamp.model;

import com.mock.nativecamp.payload.teacher.SignupReq;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.List;

@Document(collection = "Teachers")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Teachers {
    @Id
    private String userId;
    private String firstName;
    private String middleName;
    private String lastName;
    private String email;
    private String password;
    private String status;
    private String timezone;
    private String birthday;
    private String country;
    private String nationality;
    private String referrerEmail;
    private String classStatus;
    private String introductionVideo;
    private String rating;
    private String lastLogin;
    private Integer classCount;
    private Integer reservationCount;
    private Integer reservationCancelCount;
    private String selfIntroduction;
    private String employeeIntroduction;
    private String teachingYears;
    private List<String> picList;
    private List<String> badge;
    private List<String> characteristic;
    private List<String> hobby;

    public Teachers addTeachers(SignupReq signupReq) {
        Teachers teachers = new Teachers();
        teachers.setFirstName(signupReq.getFirstName());
        teachers.setMiddleName(signupReq.getMiddleName());
        teachers.setLastName(signupReq.getLastName());
        teachers.setEmail(signupReq.getEmail());
        teachers.setPassword(signupReq.getPassword());
        teachers.setBirthday(signupReq.getBirthday());
        teachers.setReferrerEmail(signupReq.getReferrerEmail());
        teachers.setCountry(signupReq.getCountry());
        teachers.setNationality(signupReq.getNationality());
        return teachers;
    }
}

然後TeachersController.java新增POST teacher、GET all teacher、GET one teacher

package com.mock.nativecamp.controller;

import com.mock.nativecamp.payload.teacher.SignupReq;
import com.mock.nativecamp.service.TeachersServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/teachers")
public class TeachersController {
    private final TeachersServer teachersServer;

    public TeachersController(TeachersServer teachersServer) {
        this.teachersServer = teachersServer;
    }

    /**
     * Signup teacher
     * @param signupReq
     * @return AdminUser
     * @throws Exception
     */
    @PostMapping(path = "/signup", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Object SignupUser(@RequestBody SignupReq signupReq) throws Exception {
        return teachersServer.signupTeacher(signupReq);
    }

    /**
     * Get all Teachers
     * @return Teachers list
     * @throws Exception
     */
    @GetMapping(path = "/all", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object GetAllTeachers() throws Exception {
        return teachersServer.getAllTeachers();
    }

    /**
     * Get one Teacher
     * @return Teachers
     * @throws Exception
     */
    @GetMapping(path = "/teacher", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object GetTeacher(@RequestParam String id) throws Exception {
        return teachersServer.getTeacher(id);
    }
}

teachersServer.java還有TeachersRepository.java中加入相對應的code。
teachersServer.java

package com.mock.nativecamp.service;

import com.mock.nativecamp.model.Teachers;
import com.mock.nativecamp.payload.teacher.SignupReq;
import com.mock.nativecamp.repository.TeachersRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class TeachersServer {

    @Autowired
    private TeachersRepository teachersRepository;

    public Object signupTeacher(SignupReq signupReq) {
        Teachers teachers = new Teachers().addTeachers(signupReq);
        teachersRepository.save(teachers);
        return new ResponseEntity(HttpStatus.ACCEPTED);
    }

    public Object getAllTeachers() {
        return teachersRepository.findAll();
    }

    public Object getTeacher(String id) {
        return teachersRepository.findByUserId(id);
    }
}

TeachersRepository.java

package com.mock.nativecamp.repository;

import com.mock.nativecamp.model.Teachers;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface TeachersRepository extends MongoRepository<Teachers, String>{
    Teachers findByUserId(String id);
}

接下來就只剩patch了,關於patch因為我是偷懶的設計,由前端帶完整資料過來,直接做更新,所以不應該在model中將原本NonNull的欄位拿掉,應該要保持原有的規則,所以修改後的teacher PatchReq.java

package com.mock.nativecamp.payload.teacher;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;

@Data
@AllArgsConstructor()
public class PatchReq {
    @NonNull
    private String firstName;
    private String middleName;
    @NonNull
    private String lastName;
    @NonNull
    private String email;
    @NonNull
    private String birthday;
    @NonNull
    private String country;
    @NonNull
    private String nationality;
}

新增教師
https://ithelp.ithome.com.tw/upload/images/20210927/201403589dVpEN9IFu.png
查詢所有教師
https://ithelp.ithome.com.tw/upload/images/20210927/201403586wB9TP2iYe.png
查詢單一教師
https://ithelp.ithome.com.tw/upload/images/20210927/201403584VY0fGxjfp.png

明天再來繼續完成更新的API,真的很佩服時間管理大師,怎麼有辦法在一天的時間內處理好這麼多事情啊?


上一篇
[Day 11]在你順利的時候來一拳才是標配(前端篇)
下一篇
[Day 13]每天前進一點應該也是進步吧?(後端篇)
系列文
關於我快30歲的後端工程師,想轉職成全端工程師,在前端世界中尋求機會的那件事(後端篇)18
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言